﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam2.Biz;
namespace MockPrjTeam2.Pages.ControlOrg
{
    public partial class DirectorateListFrame : System.Web.UI.UserControl
    {
        private DirectorateBiz directorateBiz;
        public int orgId;
        private static int check;
        private static int flag;
        private static string key = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            directorateBiz = new DirectorateBiz();
            if (!int.TryParse(Request.Params["id"], out orgId))
            {
                Response.Redirect("../Forms/AddSupportingMaterial.aspx");
            }
            
            if (!IsPostBack)
            {
                this.LoadDirectorate();
                check = 0;
                flag = 0;
            }
        }

        private void LoadDirectorate()
        {
            if (int.TryParse(Request.Params["id"], out orgId))
            {
               // HiddenFielorgId.Value =Convert.ToString(orgId);
                GridViewDirectorate.DataSource = directorateBiz.GetDirectorateByorgId(orgId);
                GridViewDirectorate.DataBind();
            }
        }

        protected void LinkButtonCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "../Forms/AddDirectorate.aspx?organisationId={0}",orgId));
        }
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {

                   check = 1;
                   key = e.CommandName;
                   // int ord =Convert.ToInt32(HiddenFielorgId.Value);
                    GridViewDirectorate.DataSource = directorateBiz.FilterDirectorateByLetters(key,orgId,flag);

                    GridViewDirectorate.DataBind();
                
            
        }

        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {

            if (check == 1)
            {

                if (CheckBoxInactive.Checked && check == 1)
                {
                    flag = 1;
                    GridViewDirectorate.DataSource = directorateBiz.FilterDirectorateByLetters(key,orgId,flag);
                    
                    GridViewDirectorate.DataBind();

                }

                else if (CheckBoxInactive.Checked == false && check == 1)
                {

                    flag = 0;
                    GridViewDirectorate.DataSource = directorateBiz.FilterDirectorateByLetters(key,orgId, flag);
                   
                    GridViewDirectorate.DataBind();

                }

            }
            else if (check == 0)
            {

                if (CheckBoxInactive.Checked && check == 0)
                {
                    GridViewDirectorate.DataSource = directorateBiz.GetDirectorateInacTive(orgId);
                 
                    GridViewDirectorate.DataBind();

                }

                else
                {

                    LoadDirectorate();
                }



            }

            
        }
        protected void LinkButtonDirectorateName_Command(object sender, CommandEventArgs e)
        {

            if (e.CommandArgument.Equals("False"))
            {
                int direcId = int.Parse(e.CommandName, CultureInfo.CurrentCulture);
                bool result = directorateBiz.SetActiveDirectorate(direcId, true);
            }
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "../Forms/DirectorateDetails.aspx?directorateId={0}",e.CommandName));
        }

        protected void GridViewDirectorate_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewDirectorate.PageIndex = e.NewPageIndex;
            if (check == 1)
            {

                if (CheckBoxInactive.Checked && check == 1)
                {
                    flag = 1;
                    GridViewDirectorate.DataSource = directorateBiz.FilterDirectorateByLetters(key, orgId, flag);

                    GridViewDirectorate.DataBind();

                }

                else if (CheckBoxInactive.Checked == false && check == 1)
                {

                    flag = 0;
                    GridViewDirectorate.DataSource = directorateBiz.FilterDirectorateByLetters(key, orgId, flag);

                    GridViewDirectorate.DataBind();

                }

            }
            else if (check == 0)
            {

                if (CheckBoxInactive.Checked && check == 0)
                {
                    GridViewDirectorate.DataSource = directorateBiz.GetDirectorateInacTive(orgId);

                    GridViewDirectorate.DataBind();

                }

                else
                {
                    flag = 0;
                    GridViewDirectorate.DataSource = directorateBiz.GetDirectorateByorgId(orgId);
                }



            }
            GridViewDirectorate.DataBind();
        }
     
    }
}